package me.spring.cloud.components.starter.rpc.auth.configuration;

import me.spring.cloud.common.components.auth.client.AuthFilter;
import me.spring.cloud.common.components.auth.client.RereadFilter;
import javax.servlet.Filter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author luffy
 */
@Configuration
public class RpcAuthConfiguration {

  private static final String API = "/api/*";

  @Value("${common_auth_secret}")
  private  String secret;

  @Bean
  public Filter rereadFilter() {
    return new RereadFilter();
  }

  @Bean
  public Filter authFilter() {
    return new AuthFilter(secret);
  }

  @Bean
  public FilterRegistrationBean constructRereadFilter() {
    FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
    filterRegistrationBean.setFilter(rereadFilter());
    filterRegistrationBean.addUrlPatterns(API);
    //order的数值越小 则优先级越高
    filterRegistrationBean.setOrder(10);
    return filterRegistrationBean;
  }

  @Bean
  public FilterRegistrationBean constructAuthfilter() {
    FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
    filterRegistrationBean.setFilter(authFilter());
    filterRegistrationBean.addUrlPatterns(API);
    filterRegistrationBean.setOrder(20);
    return filterRegistrationBean;
  }
}